package com.chagee.store.operation.server.controller;

import com.alibaba.fastjson.JSON;
import com.chagee.base.utils.HttpResponse;
import com.chagee.base.utils.HttpResponseUtil;
import com.chagee.store.operation.api.request.OrderDeviceInfoRequest;
import com.chagee.store.operation.api.request.OrderInfoRequest;
import com.chagee.store.operation.api.request.StoreInfoRequest;
import com.chagee.store.operation.api.request.StoreOrderDeviceRequest;
import com.chagee.store.operation.api.response.StoreOderDeviceResponse;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.utils.CommonUtil;
import com.chagee.store.operation.server.facade.StoreOrderDeviceFacade;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author : hk
 * @Date : 2024/7/10
 */

@Slf4j
@RestController
@RequestMapping("/oderDevice")
public class StoreOrderDeviceController {

	@Resource
	private StoreOrderDeviceFacade storeOrderDeviceFacade;

	@PostMapping("/deviceConfig")
	@ApiOperation("待确认/已确认设备配置")
	public HttpResponse<List<StoreOderDeviceResponse>> deviceConfig(@RequestBody @Valid StoreOrderDeviceRequest request) {
		log.info("StoreOrderDeviceController#deviceConfig request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		List<StoreOderDeviceResponse> list = storeOrderDeviceFacade.deviceConfig(request);
		return HttpResponseUtil.success(list);
	}

	@PostMapping("/submitDeviceConfig")
	@ApiOperation("提交交接配置")
	public HttpResponse<Boolean> submitDeviceConfig(@RequestBody @Valid OrderInfoRequest request) {
		log.info("store submitDeviceConfig request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
//		boolean b = request.getOrderDeviceList().stream().anyMatch(s -> StringUtils.equals("0", s.getOperation().toString()));
//		if (b && StringUtils.isBlank(request.getTransfer())) {
//			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), "设备运行异常,门店交接信息不可为空");
//		}
		Boolean result = storeOrderDeviceFacade.submitDeviceConfig(request);
		return HttpResponseUtil.success(result);
	}

	@PostMapping("/saveDeviceConfig")
	@ApiOperation("保存交接配置")
	public HttpResponse<String> saveDeviceConfig(@RequestBody @Valid OrderDeviceInfoRequest request) {
		log.info("store saveDeviceConfig request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		String result = storeOrderDeviceFacade.saveDeviceConfig(request);
		return HttpResponseUtil.success(result);
	}

	@PostMapping("/recentlyConfig")
	@ApiOperation("沿用前次配置信息")
	public HttpResponse<List<StoreOderDeviceResponse>> recentlyConfig(@RequestBody @Valid StoreInfoRequest request) {
		log.info("store recentlyConfig request:{}, auth:{}", JSON.toJSONString(request), JSON.toJSONString(AuthUtil.getUser()));
		if (StringUtils.isNotBlank(CommonUtil.validate(request))) {
			return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), CommonUtil.validate(request));
		}
		List<StoreOderDeviceResponse> list = storeOrderDeviceFacade.recentlyConfig(request);
		return HttpResponseUtil.success(list);

	}
}
